<template>
  <div class="activity-check-container">
    <!-- 搜索区域 -->
    <div class="search-area">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="活动标题">
          <el-input v-model="searchForm.title" placeholder="请输入活动标题" clearable />
        </el-form-item>
        <el-form-item label="校区名称">
          <el-input v-model="searchForm.campusName" placeholder="请输入校区名称" clearable />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.activityStatus" placeholder="请选择状态" clearable style="width: 120px;">
            <el-option label="草稿" value="草稿" />
            <el-option label="待审核" value="待审核" />
            <el-option label="已通过" value="已通过" />
            <el-option label="已拒绝" value="已拒绝" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch" style="width: 80px;">查询</el-button>
          <el-button @click="resetSearch" style="width: 80px;">重置刷新</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <div class="table-area">
      <el-table :data="tableData.records" border stripe style="width: 100%" v-loading="loading">
        <el-table-column prop="activityId" label="活动ID" width="80" align="center" />
        <el-table-column prop="title" label="活动标题" width="200" align="center" />
        <el-table-column prop="description" label="活动描述" width="250" align="center" show-overflow-tooltip />
        <el-table-column label="时间" width="200" align="center">
          <template #default="{ row }">
            <div>开始: {{ formatDateTime(row.startTime) }}</div>
            <div>结束: {{ formatDateTime(row.endTime) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="location" label="活动地点" width="150" align="center" />
        <el-table-column label="参与人数" width="120" align="center">
          <template #default="{ row }">
            <div>当前: {{ row.currentParticipants }}</div>
            <div>最大: {{ row.maxParticipants }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="campusName" label="校区" width="120" align="center" />
        <el-table-column prop="activityStatus" label="状态" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="getStatusTagType(row.activityStatus)" style="width: 60px;height: 30px;font-size: 14px;">
              {{ row.activityStatus }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="160" align="center">
          <template #default="{ row }">
            <el-button 
              v-if="row.activityStatus === '待审核' || row.activityStatus === '已拒绝'" 
              size="small" 
              type="success" 
              @click="handleApprove(row)"
              style="width: 60px;height: 30px;font-size: 14px;"
            >
              通过
            </el-button>
            <el-button 
              v-if="row.activityStatus === '待审核' || row.activityStatus === '已通过'" 
              size="small" 
              type="danger" 
              @click="handleReject(row)"
              style="width: 60px;height: 30px;font-size: 14px;"
            >
              拒绝
            </el-button>
          </template>
        </el-table-column>
        <el-table-column prop="rejectReason" label="拒绝原因" width="200" align="center" fixed="right"/>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-area">
        <el-pagination 
          v-model:current-page="pagination.currentPage" 
          v-model:page-size="pagination.pageSize"
          :page-sizes="[5, 10, 20, 30]" 
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total" 
          @size-change="handleSizeChange" 
          @current-change="handleCurrentChange" 
        />
      </div>
    </div>

    <!-- 拒绝原因对话框 -->
    <el-dialog v-model="rejectDialogVisible" title="输入拒绝原因" width="500px">
      <el-form :model="rejectForm" label-width="100px">
        <el-form-item label="拒绝原因">
          <el-input 
            v-model="rejectForm.rejectReason" 
            type="textarea" 
            :rows="4" 
            placeholder="请输入拒绝原因"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="rejectDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmReject">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getAllActivityInfo, updateActivity } from '@/api/check'

// 数据定义
const loading = ref(false)
const rejectDialogVisible = ref(false)
const currentActivityId = ref('')

// 搜索表单
const searchForm = reactive({
  title: '',
  campusName: '',
  activityStatus: ''
})

// 拒绝表单
const rejectForm = reactive({
  rejectReason: ''
})

// 分页数据
const pagination = reactive({
  currentPage: 1,
  pageSize: 10
})

// 表格数据
const tableData = ref({
  records: [],
  total: 0
})

// 状态标签类型映射
const statusTagType = {
  '草稿': 'info',
  '待审核': 'warning',
  '已通过': 'success',
  '已拒绝': 'danger'
}

// 初始化加载数据
onMounted(() => {
  fetchActivityList()
})

// 获取活动列表
const fetchActivityList = async () => {
  try {
    loading.value = true
    const params = {
      title: searchForm.title,
      campusName: searchForm.campusName,
      activityStatus: searchForm.activityStatus,
      currentPage: pagination.currentPage,
      pageSize: pagination.pageSize
    }

    const response = await getAllActivityInfo(params)

    if (response.code === 200) {
      tableData.value = response.data.list || { records: [], total: 0 }
    } else {
      tableData.value = { records: [], total: 0 }
      ElMessage.warning(response.message || '获取活动列表失败')
    }
  } catch (error) {
    console.error('获取活动列表失败:', error)
    ElMessage.error('获取活动列表失败')
    tableData.value = { records: [], total: 0 }
  } finally {
    loading.value = false
  }
}

// 格式化日期时间
const formatDateTime = (dateStr) => {
  if (!dateStr) return ''
  try {
    const date = new Date(dateStr)
    return date.toLocaleString()
  } catch (e) {
    return dateStr
  }
}

// 获取状态标签类型
const getStatusTagType = (status) => {
  return statusTagType[status] || 'info'
}

// 搜索活动
const handleSearch = () => {
  pagination.currentPage = 1
  fetchActivityList()
}

// 重置搜索
const resetSearch = () => {
  searchForm.title = ''
  searchForm.campusName = ''
  searchForm.activityStatus = ''
  handleSearch()
}

// 分页大小变化
const handleSizeChange = (val) => {
  pagination.pageSize = val
  fetchActivityList()
}

// 当前页变化
const handleCurrentChange = (val) => {
  pagination.currentPage = val
  fetchActivityList()
}

// 通过审核
const handleApprove = async (row) => {
  try {
    await ElMessageBox.confirm('确定要通过该活动吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })

    const response = await updateActivity({
      activityId: row.activityId,
      activityStatus: '已通过',
      rejectReason: ''
    })

    if (response.code === 200) {
      ElMessage.success('审核通过成功')
      fetchActivityList()
    } else {
      ElMessage.error(response.message || '审核通过失败')
      fetchActivityList()
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('操作失败: ' + (error.response?.data?.message || error.message))
    }
  }
}

// 拒绝审核
const handleReject = (row) => {
  currentActivityId.value = row.activityId
  rejectForm.rejectReason = ''
  rejectDialogVisible.value = true
}

// 确认拒绝
const confirmReject = async () => {
  try {
    if (!rejectForm.rejectReason) {
      ElMessage.warning('请输入拒绝原因')
      return
    }

    const response = await updateActivity({
      activityId: currentActivityId.value,
      activityStatus: '已拒绝',
      rejectReason: rejectForm.rejectReason
    })

    if (response.code === 200) {
      ElMessage.success('拒绝成功')
      rejectDialogVisible.value = false
      fetchActivityList()
    } else {
      fetchActivityList()
      ElMessage.error(response.message || '拒绝失败')
    }
  } catch (error) {
    ElMessage.error('操作失败: ' + (error.response?.data?.message || error.message))
  }
}
</script>

<style scoped>
.activity-check-container {
  width: 100%;
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.search-area {
  margin-bottom: 20px;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}

.pagination-area {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-form-item {
  margin-right: 20px;
  margin-bottom: 0;
}
</style>